import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class ContainerX extends StatefulWidget {
  const ContainerX({
    super.key,
    this.onPressed,
    this.onLongPress,
    this.alignment,
    this.padding,
    this.color,
    this.decoration,
    this.hoverDecoration,
    this.foregroundDecoration,
    this.width,
    this.height,
    this.constraints,
    this.margin,
    this.transform,
    this.transformAlignment,
    this.child,
    this.clipBehavior = Clip.none,
  });

  final GestureTapCallback? onPressed;
  final GestureLongPressCallback? onLongPress;

  final AlignmentGeometry? alignment;
  final EdgeInsetsGeometry? padding;
  final Color? color;
  final Decoration? decoration;
  final Decoration? hoverDecoration;
  final Decoration? foregroundDecoration;
  final double? width;
  final double? height;
  final BoxConstraints? constraints;
  final EdgeInsetsGeometry? margin;
  final Matrix4? transform;
  final AlignmentGeometry? transformAlignment;
  final Clip clipBehavior;
  final Widget? child;

  @override
  State<StatefulWidget> createState() => _ContainerXState();
}

class _ContainerXState extends State<ContainerX> {
  bool _isHover = false;

  @override
  Widget build(BuildContext context) {
    Widget child = Container(
      alignment: widget.alignment,
      padding: widget.padding,
      color: widget.color,
      decoration: _isHover
          ? widget.hoverDecoration ?? widget.decoration
          : widget.decoration,
      foregroundDecoration: widget.foregroundDecoration,
      width: widget.width,
      height: widget.height,
      constraints: widget.constraints,
      transform: widget.transform,
      transformAlignment: widget.transformAlignment,
      clipBehavior: widget.clipBehavior,
      child: widget.child,
    );

    if (widget.hoverDecoration != null) {
      child = MouseRegion(
        onEnter: (_) {
          setState(() {
            _isHover = true;
          });
        },
        onExit: (_) {
          setState(() {
            _isHover = false;
          });
        },
        child: child,
      );
    }

    if (widget.onPressed != null || widget.onLongPress != null) {
      child = InkWell(
        onTap: widget.onPressed,
        onLongPress: widget.onLongPress,
        child: child,
      );
    }

    if (widget.margin != null) {
      child = Padding(padding: widget.margin ?? EdgeInsetsGeometry.zero, child: child);
    }

    return child;
  }
}
